/*
 * ============================================================================
 *
 *       Filename:  inline_assmbler.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2013年06月03日 13时54分53秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>

void add_r(int a, int b)
{
	__asm__ __volatile__("addl %%ebx,%%eax"
		: "=a"(a)
		: "a"(a),"b"(b));
	printf("add: %d\n", a);
}

void add_m(int a, int b)
{
	__asm__ __volatile__("lock; \
		addl %1,%0;"
		:"=m"(a)
		:"ir"(b), "m"(a)
		:
		);
	printf("add: %d\n", a);
}

void is_zero(int a)
{
	uint8_t cond = 1;
	__asm__ __volatile__("decl %0; sete %1"
		: "=m" (a), "=q"(cond)
		: "m"(a)
		: "memory");
	printf("a:%d, cond:%d\n", a, cond);
}

void bit_set(int a)
{
	__asm__ __volatile__("btsl %1,%0"
		: "=m"(a)
		: "Ir"(1)
		: "cc");
	printf("set first bit of a :%d\n", a);
}

int main()
{
	int x = 1;
	int five_times_x;
	/* multi 5 */
	asm("leal (%1,%1,4), %0"
		: "=r" (five_times_x)
		: "r" (x));
	printf("num: %d\n", five_times_x);

	/* multi 5 */
	asm("leal (%0,%0,4), %0"
		: "=r"(five_times_x)
		: "0" (x)
		);
	printf("num: %d\n", five_times_x);

	/* sum a b */
	add_r(1,2);
	add_m(1,2);

	/* check zero */
	is_zero(1);

	/* bit set */
	bit_set(0);
}
